﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class NewsAddControl : YuanlinUserControl
    {
        protected ValuedTextBox titleValuedTextBox;
        protected ValuedDropDownList parentNewsCategoryDropDownList;
        protected ValuedDropDownList childNewsCategoryDropDownList;
        protected CheckBoxList companyTypesCheckBoxList;
        protected ValuedEditor descriptionValuedEditor;
        protected ValuedDropDownList provinceDropDownList;
        protected ValuedDropDownList cityDropDownList;
        protected ValuedDropDownList townDropDownList;
        protected ValuedDropDownList smallTownDropDownList;
        protected HiddenField townHidden;
        protected HiddenField smallTownHidden;
        protected FileUpload attachmentData;
        protected CheckBox topCheckBox;
        protected IButton SaveButton;
        protected IButton BackButton;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                Bind();
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
            AjaxManager.Register(this, "AjaxMethod");
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }
        public void SaveButton_Click(object sender, EventArgs e)
        {
            News news = new News();

            news.Title.Value =  CensorWordManager.CensorWord(this.titleValuedTextBox.Value);
            news.Body.Value =  CensorWordManager.CensorWord(this.descriptionValuedEditor.Value);
            news.NewsCategoryId.Value = int.Parse(parentNewsCategoryDropDownList.Value);
            news.ChildNewsCategoryId.Value = int.Parse(this.Page.Request.Form[this.childNewsCategoryDropDownList.UniqueID]);
            news.IsTop.Value = topCheckBox.Checked ? 1 : 0;
            string companyTypes = string.Empty;
            foreach (ListItem item in companyTypesCheckBoxList.Items)
            {
                if (item.Selected)
                {
                    companyTypes += "," + item.Value;
                }
            }
            if (companyTypes.Length > 0)
            {
                companyTypes = companyTypes.Substring(1);
            }
            news.CompanyTypes.Value = companyTypes;
            news.CreateDate.Value = DateTime.Now;

            news.Province.Value = Convert.ToInt32(provinceDropDownList.Value);
            Area area = BusinessManager.GetArea(news.Province.Value);
            area.NewsCount.Value++;
            BusinessManager.UpdateArea(area);

            news.City.Value = Convert.ToInt32(cityDropDownList.Value);
            area = BusinessManager.GetArea(news.City.Value);
            area.NewsCount.Value++;
            BusinessManager.UpdateArea(area);

            int town = Convert.ToInt32(townDropDownList.Value);
            if (!string.IsNullOrEmpty(townHidden.Value))
            {
                town = Convert.ToInt32(townHidden.Value);
            }
            news.Town.Value = town;
            area = BusinessManager.GetArea(news.Town.Value);
            area.NewsCount.Value++;
            BusinessManager.UpdateArea(area);

            int smallTown = Convert.ToInt32(smallTownDropDownList.Value);
            if (!string.IsNullOrEmpty(smallTownHidden.Value))
            {
                smallTown = Convert.ToInt32(smallTownHidden.Value);
            }
            news.SmallTown.Value = smallTown;
            area = BusinessManager.GetArea(news.SmallTown.Value);
            area.NewsCount.Value++;
            BusinessManager.UpdateArea(area);

            news.AttachmentFileName.Value = SiteUrls.Instance().GetPostedFileName(attachmentData);

            BusinessManager.CreateNews(news);

            if (SiteUrls.Instance().IsAttachmentValid(attachmentData))
            {
                SiteUrls.Instance().SaveFile(SiteUrls.Instance().GetFile(news), attachmentData);
            }

            if (string.IsNullOrEmpty(user.Business))
            {
                user.Business = "1";
            }
            else
            {
                user.Business = (Int32.Parse(user.Business) + 1).ToString();
            }
           UserManager.UpdateUser(user);

           PageRedirect();
        }
        private void Bind()
        {
            NewsCategoryListRequest request = new NewsCategoryListRequest();
            request.UpNewsCategoryId = 0;
            EntityList newsCategories = BusinessManager.GetNewsCategoryList(RequestHelper.BuildGetNewsCategoryListRequest(request));

            parentNewsCategoryDropDownList.DataSource = newsCategories;
            parentNewsCategoryDropDownList.DataValueField = "EntityId";
            parentNewsCategoryDropDownList.DataTextField = "Title";
            parentNewsCategoryDropDownList.DataBind();

            if (this.parentNewsCategoryDropDownList.Items.Count > 0)
            {
                this.parentNewsCategoryDropDownList.Value = (newsCategories[0] as NewsCategory).EntityId.Value.ToString();
                request = new NewsCategoryListRequest();
                request.UpNewsCategoryId = (newsCategories[0] as NewsCategory).EntityId.Value;
                EntityList childCategories = BusinessManager.GetNewsCategoryList(RequestHelper.BuildGetNewsCategoryListRequest(request));
                this.childNewsCategoryDropDownList.DataSource = childCategories;
                this.childNewsCategoryDropDownList.DataValueField = "EntityId";
                this.childNewsCategoryDropDownList.DataTextField = "Title";
                this.childNewsCategoryDropDownList.DataBind();
                if (this.childNewsCategoryDropDownList.Items.Count > 0)
                {
                    this.childNewsCategoryDropDownList.Value = (childCategories[0] as NewsCategory).EntityId.Value.ToString();
                }
            }

            EntityList companys = BusinessManager.GetCompanyTypeList(RequestHelper.BuildGetCompanyTypeListRequest());
            if (companys != null)
            {
                companyTypesCheckBoxList.DataSource = companys;
                companyTypesCheckBoxList.DataValueField = "EntityId";
                companyTypesCheckBoxList.DataTextField = "Title";
                companyTypesCheckBoxList.DataBind();
            }

            AreaListRequest request1 = new AreaListRequest();
            request1.UpAreaId = 0;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request1));
            if (areas == null)
                return;
            BindDropDownList(provinceDropDownList, areas, "EntityId", "Title");
            if (this.provinceDropDownList.SelectedValue.Length == 0)
                return;

            request1 = new AreaListRequest();
            request1.UpAreaId = Convert.ToInt32(this.provinceDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request1));
            if (areas == null)
                return;
            BindDropDownList(cityDropDownList, areas, "EntityId", "Title");
            if (this.cityDropDownList.SelectedValue.Length == 0)
                return;

            request1 = new AreaListRequest();
            request1.UpAreaId = Convert.ToInt32(this.cityDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request1));
            if (areas == null)
                return;
            BindDropDownList(townDropDownList, areas, "EntityId", "Title");
            if (this.townDropDownList.SelectedValue.Length == 0)
                return;

            request1 = new AreaListRequest();
            request1.UpAreaId = Convert.ToInt32(this.townDropDownList.SelectedValue);
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request1));
            if (areas == null)
                return;
            BindDropDownList(smallTownDropDownList, areas, "EntityId", "Title");
        }
        private void BindDropDownList(ValuedDropDownList ddl, EntityList list, string valueField, string textField)
        {
            ddl.DataSource = list;
            ddl.DataValueField = valueField;
            ddl.DataTextField = textField;
            ddl.DataBind();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetNewsListUrl());
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }

            //得到子列表的集合
            NewsCategoryListRequest request = new NewsCategoryListRequest();
            request.UpNewsCategoryId = parentCategoryID;
            EntityList categories = BusinessManager.GetNewsCategoryList(RequestHelper.BuildGetNewsCategoryListRequest(request));
            if (categories.Count > 0)
            {
                string categoryFormat = "{0}:{1}";
                string categoryString = string.Empty;
                foreach (NewsCategory category in categories)
                {
                    categoryString += ";" + string.Format(categoryFormat, category.EntityId.Value.ToString(), category.Title.Value);
                }
                if (categoryString.Length > 0)
                {
                    categoryString = categoryString.Substring(1);
                }
                return categoryString;
            }

            return string.Empty;
        }
        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public string GetAreaChildCategories(int parentCategoryID)
        {
            if (parentCategoryID == -1)
            {
                return string.Empty;
            }
            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = parentCategoryID;
            EntityList areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            if (areas.Count > 0)
            {
                string areaFormat = "{0}:{1}";
                string areaString = string.Empty;
                foreach (Area area in areas)
                {
                    areaString += ";" + string.Format(areaFormat, area.EntityId.Value.ToString(), area.Title.Value);
                }
                if (areaString.Length > 0)
                {
                    areaString = areaString.Substring(1);
                }
                return areaString;
            }

            return string.Empty;
        }

        #endregion
    }
}